<template>
	<view>
		<view class="cu-card case" :class="isCard?'no-card':''">
			<view class="cu-item shadow">
				<view class="image">
					<image src="https://mp-7c5f2fa7-1d2c-41f1-b3dd-1f45e490fd36.cdn.bspapp.com/cloudstorage/681b2e59-56ee-4845-883a-7750e507cca6.jpg"
					 mode="widthFix"></image>
				</view>
			</view>
		</view>
		<view class="cu-modal" :class="modalName=='Image'?'show':''">
			<view class="cu-dialog">
				<view class="bg-img" :style="'background-image: url('+hcxx.value+');height:230px;'">
					<view class="cu-bar justify-end text-white">
						<view class="action" @tap="hideModal">
							<text class="cuIcon-close "></text>
						</view>
					</view>
				</view>
				<view class="cu-bar bg-white">
					<view class="action margin-0 flex-sub  solid-left" @tap="hideModal">溜了溜了</view>
				</view>
			</view>
		</view>
		<uni-section :title="hcxx.comment" type="line">
			<view class="example" style="padding: 10px;">
				<!-- 基础表单校验 -->
				<uni-forms ref="valiForm" :rules="rules" :model="valiFormData" labelWidth="80px">
					<uni-forms-item label="账号" required name="zjhm">
						<uni-easyinput type="test" :disabled="true" v-model="valiFormData.zjhm" placeholder="请输入账号" />
					</uni-forms-item>
				</uni-forms>
				<!-- <button :loading="loading" :disabled="loading" type="primary" @click="submit('valiForm')">提交</button> -->
				<button v-if="!valiFormData.zjhm" type="primary" open-type="getPhoneNumber"  @getphonenumber="getPhoneNumber1">获取手机号</button>
				<button v-if="valiFormData.zjhm" type="primary" @click="getNoPhoneNumber1">登录</button>
				
			</view>
		</uni-section>
	</view>
</template>
<script>
	var _this;
	var vk = uni.vk;
	import uniForms from "@/pages_bd/components/uni-forms/uni-forms";
	import uniFormsItem from "@/pages_bd/components/uni-forms-item/uni-forms-item";
	export default {
		components:{uniForms,uniFormsItem},
		data() {
			// 页面数据变量
			return {
				opid:'',
				avatar:'',
				nickname:'',
				userid:'',
				hcxx:{
					value:'',
					comment:'',
					ankz:false,
					tzgg:''
				},
				modalName: null,
				show: false,
				syimg:'https://mp-7c5f2fa7-1d2c-41f1-b3dd-1f45e490fd36.cdn.bspapp.com/cloudstorage/939374e2-cdad-4e6f-b088-9546464e94ea.jpg',
				tsxx:'',
				loading: false,
				// 校验规则
				rules: {
					zjhm: {
						rules: [{
							required: true,
							errorMessage: '账号错误'
						}]
					}
				},
				valiFormData:{zjhm:""},
				isCard: false,
				// init请求返回的数据
				data:{
					
				},
				// 表单请求数据
				form1:{
					
				},
				scrollTop:0,
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		async onLoad(options = {}) {
			vk = uni.vk;
			_this = this;
			this.options = options;
			this.init(options);
			//await this.getvkGlobalData();
			await this.getxxcjcxglobal()
			let userinfo = vk.getVuex('$user.userInfo');
			_this.opid = vk.getVuex('$user.userInfo.wx_openid.mp-weixin');
			_this.avatar = vk.getVuex('$user.userInfo.avatar');
			_this.nickname = vk.getVuex('$user.userInfo.nickname');
			_this.userid = vk.getVuex('$user.userInfo._id');

			const value = uni.getStorageSync('xmt-xs-dl-key');
			if (value) {
				this.valiFormData.zjhm = value.xmtxszh;
			}
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady(){
			
		},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {
			
		},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {
			
		},
		// 监听 - 点击右上角转发时
		onShareAppMessage(options) {
			
		},
		// 函数
		methods: {
			dyxx(){
				uni.requestSubscribeMessage({
				  tmplIds: ['FII6eFIVp7TYzIDchcREgsA9Nj9JffLL_4u6ZAgikyo'],
				  success (res) {
					  //console.log(res) 订阅新媒体打卡
				  },fail(fail){
					   //console.log(fail)
				  }
				})
			},
			async getNoPhoneNumber1(){
				uni.login({
						"provider": "weixin",
						"onlyAuthorize": true, // 微信登录仅请求授权认证
						success: async function(event){
							const {code} = event
							await vk.callFunction({
							  url: 'client/gsxy/xmtdk/pub/xmtdkapi.getxmtopid',
							  title:'请求中...',
							  data:{code:code}
							}).then((data) => {
							  vk.setVuex('$user.userInfo.wx_openid.mp-weixin',data.code2SessionRes.openid);
							}).catch((err) => {});
						},
						fail: function (err) {}
					})
				await this.getxmtxsinfo(_this.valiFormData.zjhm)
			},
			getPhoneNumber1(e){
				let that = this;
				// 微信新增了code参数，可以直接传code，不再需要传 encryptedData 和 iv
				let { code } = e.detail;
				if (!code) {
					return false;
				}
				vk.userCenter.getPhoneNumber({
					data:{
						code,
						encryptedKey: that.encryptedKey
					},
					success: async(data) => {
						_this.valiFormData.zjhm = data.phone

					}
				});
			},
			randomChoice(arr) {
			 return arr[Math.floor(Math.random() * arr.length)];
			},
			getxxcjcxglobal(){
				return vk.callFunction({
				  url: 'client/gsxy/globalDataCacheKye/pub/vk-global-data.findById',
				  title:'请求中...',
				  data:{
				    _id:'65a645dba7c432e4a3a10635'
				  }
				}).then((data) => {
					_this.hcxx = data.info;
				}).catch((err) => {});
			},
			getvkGlobalData(){
				// promise方式
				return vk.callFunction({
				  url: 'client/gsxy/xmtdk/pub/xmtdk-xs-api.getvkGlobalData',
				  title:'请求中...',
				  data:{}
				}).then((data) => {
					_this.hcxx = data.info;
				}).catch((err) => {
				  
				});
			},
			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
				//Image
			},
			hideModal(e) {
				this.modalName = null
			},
			containsAtSign(str) {
			 return str.includes('@');
			},
			getxmtxsinfo(zjhm){
				return vk.callFunction({
				  url: 'client/gsxy/xmtdk/pub/xmtdk-xs-api.getxmtxsinfo',
				  title:'请求中...',
				  data:{
				    xmtxszh:zjhm,
					opid:_this.opid,
					avatar:_this.avatar,
					nickname:_this.nickname,
					userid:_this.userid
				  }
				}).then((data) => {
					if(data.info){
						uni.setStorageSync('xmt-xs-dl-key',data.info);
						uni.navigateTo({
							url: '/pages_bd/xmt_xs/xmtdk-xs-list'
						})
						//this.dyxx();
					}else{
						this.modalName = 'Image';
					}
				}).catch((err) => {
				  console.log(err)
				});
			},
			submit(ref) {
				_this.loading = true
					this.$refs[ref].validate().then(async res => {
						if(res.zjhm){
							//1.查出数据
							await _this.getxmtxsinfo(res.zjhm);
							//2.进入任务列表页面
							//uni.setStorageSync('xmt-xs-dl-key',res.zjhm);
							
						}else{
							
							this.modalName = 'Image';
						}
						_this.loading = false;
					}).catch(err => {
						uni.showToast({
							title: err[0].errorMessage,
							icon:'none'
						})
						_this.loading = false;
					})
			},
			IsCard(e) {
				this.isCard = e.detail.value
			},
			// 页面数据初始化函数
			init(options){
				//vk.reLaunch("/pages_template/uni-id/index/index");
			},
			pageTo(path){
				vk.navigateTo(path);
			}
		},
		// 监听器
		watch:{
			
		},
		// 计算属性
		computed:{
			
		}
	}
</script>
<style lang="scss" scoped>
	
	
</style>